<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{{ post.title }} - 环保社区</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }

        .container {
            max-width: 900px;
            margin: 0 auto;
            background: white;
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.1);
            overflow: hidden;
        }

        .header {
            background: linear-gradient(135deg, #4ECDC4 0%, #44A08D 100%);
            color: white;
            padding: 30px;
            text-align: center;
        }

        .header h1 {
            font-size: 2.5em;
            margin-bottom: 10px;
        }

        .content {
            padding: 30px;
        }

        .back-btn {
            display: inline-block;
            background: #6c757d;
            color: white;
            text-decoration: none;
            padding: 10px 20px;
            border-radius: 5px;
            margin-bottom: 30px;
            transition: all 0.3s;
        }

        .back-btn:hover {
            background: #5a6268;
            text-decoration: none;
            color: white;
        }

        .post-container {
            background: white;
            border: 1px solid #e9ecef;
            border-radius: 10px;
            padding: 30px;
            margin-bottom: 30px;
        }

        .post-header {
            margin-bottom: 25px;
            padding-bottom: 20px;
            border-bottom: 1px solid #e9ecef;
        }

        .post-category {
            display: inline-block;
            background: #4ECDC4;
            color: white;
            padding: 6px 15px;
            border-radius: 20px;
            font-size: 14px;
            margin-bottom: 15px;
        }

        .post-title {
            font-size: 2em;
            font-weight: bold;
            color: #333;
            margin-bottom: 15px;
            line-height: 1.3;
        }

        .post-meta {
            display: flex;
            justify-content: space-between;
            align-items: center;
            color: #666;
            font-size: 14px;
        }

        .post-author {
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .author-avatar {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            background: linear-gradient(135deg, #4ECDC4 0%, #44A08D 100%);
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-weight: bold;
            font-size: 16px;
        }

        .post-stats {
            display: flex;
            gap: 20px;
            align-items: center;
        }

        .stat-item {
            display: flex;
            align-items: center;
            gap: 5px;
        }

        .post-content {
            font-size: 16px;
            line-height: 1.8;
            color: #333;
            margin-bottom: 25px;
            white-space: pre-wrap;
        }

        .post-actions {
            display: flex;
            gap: 15px;
            align-items: center;
            padding-top: 20px;
            border-top: 1px solid #e9ecef;
        }

        .like-btn {
            background: none;
            border: 2px solid #e9ecef;
            color: #666;
            padding: 10px 20px;
            border-radius: 25px;
            cursor: pointer;
            display: flex;
            align-items: center;
            gap: 8px;
            transition: all 0.3s;
            font-size: 14px;
        }

        .like-btn:hover {
            border-color: #4ECDC4;
            color: #4ECDC4;
        }

        .like-btn.liked {
            background: #e74c3c;
            border-color: #e74c3c;
            color: white;
        }

        .replies-section {
            margin-top: 40px;
        }

        .replies-title {
            font-size: 1.5em;
            font-weight: bold;
            margin-bottom: 25px;
            color: #333;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .reply-form {
            background: #f8f9fa;
            padding: 25px;
            border-radius: 10px;
            margin-bottom: 30px;
            border: 1px solid #e9ecef;
        }

        .reply-form h3 {
            margin-bottom: 15px;
            color: #333;
        }

        .reply-textarea {
            width: 100%;
            min-height: 120px;
            padding: 15px;
            border: 2px solid #e9ecef;
            border-radius: 8px;
            font-size: 14px;
            font-family: inherit;
            resize: vertical;
            transition: all 0.3s;
        }

        .reply-textarea:focus {
            outline: none;
            border-color: #4ECDC4;
            box-shadow: 0 0 10px rgba(78, 205, 196, 0.2);
        }

        .reply-actions {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-top: 15px;
        }

        .char-counter {
            font-size: 12px;
            color: #666;
        }

        .char-counter.warning {
            color: #e74c3c;
        }

        .reply-btn {
            background: linear-gradient(135deg, #4ECDC4 0%, #44A08D 100%);
            color: white;
            border: none;
            padding: 10px 25px;
            border-radius: 25px;
            cursor: pointer;
            transition: all 0.3s;
        }

        .reply-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(78, 205, 196, 0.3);
        }

        .reply-btn:disabled {
            background: #6c757d;
            cursor: not-allowed;
            transform: none;
            box-shadow: none;
        }

        .replies-list {
            space-y: 20px;
        }

        .reply-item {
            background: white;
            border: 1px solid #e9ecef;
            border-radius: 10px;
            padding: 20px;
            margin-bottom: 15px;
            transition: all 0.3s;
        }

        .reply-item:hover {
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }

        .reply-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 15px;
        }

        .reply-author {
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .reply-avatar {
            width: 35px;
            height: 35px;
            border-radius: 50%;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-weight: bold;
            font-size: 14px;
        }

        .reply-content {
            color: #333;
            line-height: 1.6;
            margin-bottom: 15px;
            white-space: pre-wrap;
        }

        .reply-actions-bar {
            display: flex;
            justify-content: space-between;
            align-items: center;
            font-size: 12px;
            color: #999;
        }

        .reply-like-btn {
            background: none;
            border: none;
            color: #999;
            cursor: pointer;
            display: flex;
            align-items: center;
            gap: 5px;
            padding: 5px;
            border-radius: 5px;
            transition: all 0.3s;
        }

        .reply-like-btn:hover {
            background: #f8f9fa;
            color: #4ECDC4;
        }

        .reply-like-btn.liked {
            color: #e74c3c;
        }

        .empty-replies {
            text-align: center;
            padding: 40px;
            color: #666;
            font-size: 16px;
        }

        .error-message {
            background: #f8d7da;
            color: #721c24;
            padding: 15px;
            border-radius: 5px;
            margin-bottom: 20px;
            border: 1px solid #f5c6cb;
        }

        .success-message {
            background: #d4edda;
            color: #155724;
            padding: 15px;
            border-radius: 5px;
            margin-bottom: 20px;
            border: 1px solid #c3e6cb;
        }

        @media (max-width: 768px) {
            .post-meta {
                flex-direction: column;
                align-items: flex-start;
                gap: 10px;
            }
            
            .post-stats {
                gap: 15px;
            }
            
            .post-actions {
                flex-direction: column;
                align-items: stretch;
            }
            
            .reply-actions {
                flex-direction: column;
                align-items: stretch;
                gap: 10px;
            }
            
            .reply-header {
                flex-direction: column;
                align-items: flex-start;
                gap: 10px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>帖子详情</h1>
            <p>查看详细内容和参与讨论</p>
        </div>

        <div class="content">
            <a href="{{ url_for('forum') }}" class="back-btn">← 返回社区</a>
            
            {% with messages = get_flashed_messages(with_categories=true) %}
                {% if messages %}
                    {% for category, message in messages %}
                        <div class="{% if category == 'success' %}success-message{% else %}error-message{% endif %}">
                            {{ message }}
                        </div>
                    {% endfor %}
                {% endif %}
            {% endwith %}

            <div class="post-container">
                <div class="post-header">
                    <div class="post-category">{{ post.category }}</div>
                    <h1 class="post-title">{{ post.title }}</h1>
                    
                    <div class="post-meta">
                        <div class="post-author">
                            <div class="author-avatar">{{ post.author.username[0].upper() }}</div>
                            <div>
                                <div><strong>{{ post.author.username }}</strong></div>
                                <div>{{ post.created_at.strftime('%Y-%m-%d %H:%M') }}</div>
                            </div>
                        </div>
                        
                        <div class="post-stats">
                            <div class="stat-item">
                                👁️ {{ post.views_count }}
                            </div>
                            <div class="stat-item">
                                💬 {{ post.reply_count }}
                            </div>
                        </div>
                    </div>
                </div>
                
                <div class="post-content">
                    {{ post.content }}
                </div>
                
                <div class="post-actions">
                    <button class="like-btn" id="postLikeBtn" onclick="togglePostLike({{ post.id }})">
                        ❤️ <span id="postLikesCount">{{ post.like_count }}</span>
                    </button>
                </div>
            </div>

            <div class="replies-section">
                <div class="replies-title">
                    💬 回复 ({{ replies|length }})
                </div>
                
                {% if current_user %}
                <div class="reply-form">
                    <h3>发表回复</h3>
                    <form method="POST" id="replyForm">
                        <textarea id="replyContent" name="content" class="reply-textarea" 
                                  placeholder="写下你的回复..." required maxlength="1000"></textarea>
                        <div class="reply-actions">
                            <div class="char-counter" id="replyCounter">0/1000</div>
                            <button type="submit" class="reply-btn">发表回复</button>
                        </div>
                    </form>
                </div>
                {% else %}
                <div class="reply-form">
                    <p>请 <a href="{{ url_for('login') }}">登录</a> 后参与讨论</p>
                </div>
                {% endif %}
                
                <div class="replies-list">
                    {% for reply in replies %}
                    <div class="reply-item">
                        <div class="reply-header">
                            <div class="reply-author">
                                <div class="reply-avatar">{{ reply.author.username[0].upper() }}</div>
                                <div>
                                    <strong>{{ reply.author.username }}</strong>
                                    <div style="font-size: 12px; color: #999;">
                                        {{ reply.created_at.strftime('%Y-%m-%d %H:%M') }}
                                    </div>
                                </div>
                            </div>
                        </div>
                        
                        <div class="reply-content">
                            {{ reply.content }}
                        </div>
                        
                        <div class="reply-actions-bar">
                            <div></div>
                            <button class="reply-like-btn" onclick="toggleReplyLike({{ reply.id }}, this)">
                                ❤️ <span>{{ reply.like_count }}</span>
                            </button>
                        </div>
                    </div>
                    {% endfor %}
                    
                    {% if not replies %}
                    <div class="empty-replies">
                        <p>还没有回复，快来抢沙发吧！</p>
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>

    <script>
        // 字符计数器
        document.getElementById('replyContent').addEventListener('input', function() {
            const content = this.value;
            const counter = document.getElementById('replyCounter');
            const currentLength = content.length;
            
            counter.textContent = `${currentLength}/1000`;
            
            if (currentLength > 900) {
                counter.classList.add('warning');
            } else {
                counter.classList.remove('warning');
            }
        });

        // 帖子点赞
        function togglePostLike(postId) {
            fetch(`/forum/like/${postId}`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                }
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    const countSpan = document.getElementById('postLikesCount');
                    const button = document.getElementById('postLikeBtn');
                    
                    countSpan.textContent = data.likes_count;
                    
                    if (data.liked) {
                        button.classList.add('liked');
                    } else {
                        button.classList.remove('liked');
                    }
                } else {
                    alert(data.error || '操作失败');
                }
            })
            .catch(error => {
                console.error('点赞失败:', error);
                alert('操作失败，请稍后重试');
            });
        }

        // 回复点赞
        function toggleReplyLike(replyId, button) {
            fetch(`/forum/reply/like/${replyId}`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                }
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    const countSpan = button.querySelector('span');
                    countSpan.textContent = data.likes_count;
                    
                    if (data.liked) {
                        button.classList.add('liked');
                    } else {
                        button.classList.remove('liked');
                    }
                } else {
                    alert(data.error || '操作失败');
                }
            })
            .catch(error => {
                console.error('点赞失败:', error);
                alert('操作失败，请稍后重试');
            });
        }

        // 表单验证
        document.getElementById('replyForm').addEventListener('submit', function(e) {
            const content = document.getElementById('replyContent').value.trim();
            
            if (!content) {
                alert('请输入回复内容');
                e.preventDefault();
                return;
            }
            
            if (content.length > 1000) {
                alert('回复内容不能超过1000字符');
                e.preventDefault();
                return;
            }
        });

        // 页面加载时增加浏览量
        window.onload = function() {
            fetch(`/forum/post/{{ post.id }}/view`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                }
            }).catch(error => {
                console.log('更新浏览量失败:', error);
            });
        };
    </script>
</body>
</html>